package com.zmn.oms.admin.controller.perf.city;

import com.zmn.common.dto.LayuiTableResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.common.utils.office.excel.ExportExcelBuilder;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.permit.client.annotation.FilterPermitData;
import com.zmn.oms.admin.controller.perf.PerfBaseController;
import com.zmn.oms.business.interfaces.perf.PerfCityBService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.perf.PerfCityQuery;
import com.zmn.oms.model.vo.perf.PerfCityVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 城市绩效
 *
 * @author wangjie
 * @date 14:54 2019/3/2
 **/
@RestController
@RequestMapping(value = "/perf/city")
public class PerfCityController extends PerfBaseController {

    @Resource
    PerfCityBService perfCityBService;

    /**
     * 描述: 城市绩效统计主页
     *
     * @param modelAndView 视图模型返回
     * @return org.springframework.web.servlet.ModelAndView
     * @author wangjie
     * @date 16:48 2019/3/1
     **/
    @RequestMapping(value = "/list")
    public ModelAndView list(ModelAndView modelAndView) {
        modelAndView.setViewName("perf/city/list");
        return modelAndView;
    }

    /**
     * 描述: 城市绩效统计主页 ajax请求
     *
     * @param query 城市绩效统计 查询条件
     * @return com.zmn.common.dto.LayuiTableResponseDTO
     * @author wangjie
     * @date 16:48 2019/3/1
     **/
    @FilterPermitData
    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public LayuiTableResponseDTO ajaxList(PerfCityQuery query) {
        if (Objects.isNull(query.getMonth())) {
            query.setMonth(Integer.parseInt(DateUtil.getNowFormatted(DateUtil.FORMAT_MONTH2)));
        }
        List<PerfCityVO> perfCityVOS = perfCityBService.listPageByQuery(query);
        LayuiTableResponseDTO<PerfCityVO> success = LayuiTableResponseDTO.success(query.getTotalCount(), perfCityVOS);
        try {
            success.setExtData(perfCityBService.getTotalLine(query));
        } catch (Exception e) {
            e.printStackTrace();
            LoggerUtil.error(this.getClass(), e.getMessage(), e);
        }
        return success;
    }

    /**
     * 描述: 根据条件导出城市绩效Excel
     *
     * @param query 查询条件
     * @author wangjie
     * @date 11:43 2019/2/19
     **/
    @FilterPermitData
    @RequestMapping(value = "/outputExcel")
    public void outputExcel(PerfCityQuery query,HttpServletResponse response) throws OmsBaseException {
        LoginStaff currentStaff = getCurrentStaff();
        Integer exportLimit = currentStaff.getExportLimit();
        query.setPageIndex(1);
        query.setPageSize(exportLimit);
        if (Objects.isNull(query.getMonth())) {
            query.setMonth(Integer.parseInt(DateUtil.getNowFormatted(DateUtil.FORMAT_MONTH2)));
        }
        ExportExcelBuilder exportExcelBuilder = perfCityBService.outputExcel(query);
        try {
            exportExcelBuilder.exportExcel(response, "城市绩效导出");
        } catch (Exception e) {
            throw new OmsBaseException(e.getMessage());
        }
    }
}
